package com.lenovo.study.controller;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

/**
 * Author: fuxh3@lenovo.com
 * Date: Created in 2017/9/1 0001
 */
@RestController public class LoginController {
    private static final Logger logger = LogManager.getLogger(LoginController.class);
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestParam(value = "username", required = true) String userName,
                                    @RequestParam(value = "password", required = true)
                                    String password,
                                    @RequestParam(value = "rememberMe", required = true, defaultValue = "false")
                                    boolean rememberMe) {
        logger.info("==========" + userName + password + rememberMe);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
        token.setRememberMe(rememberMe);
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return "{\"Msg\":\"您的账号或密码输入错误\",\"state\":\"failed\"}";
        }
        return "{\"Msg\":\"登陆成功\",\"state\":\"success\"}";
    }

    @RequestMapping("/") @ResponseBody public String index() {
        return "no permission";
    }
}
